<?php
namespace Admin\Controller;

use Think\Controller;

class QueryController extends HomeController
{

    public function _initialize()
    {
        $storeList = D('Stores')->getStoresAllList();
        $this->assign('storeList', $storeList);
    }

    /*
     * Function:        classOrderList
     * Description:    用户小班课列表
     */
    public function classOrderList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $cate_id = 1;
        if (!empty($cate_id)) {
            $filter['cate_id'] = $cate_id;
        }
        $filter['course_id'] = I('course_id/d', '');
        if (!empty($filter['export'])) {
            D('UserCourse')->exportUserCourse($filter);
            exit;
        }
        $filter = returnFilter($filter);
        $list = D('UserCourse')->getUserCourseList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $val['user_name'] = M('user')->where(['id' => $val['user_id']])->getField('username');
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
                $userCardInfo = M('user_card')->where(['id' => $val['user_card_id']])->find();
                $val['goods_title'] = M('goods')->where(['id' => $userCardInfo['goods_id']])->getField('goods_title');
            }
        }
        $count = D('UserCourse')->getUserCourseCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $coachList = M('stores_worker_list')->where(['role' => 1, 'status' => 1, 'is_deleted' => 0])->select();
        $this->assign('coachList', $coachList);
        $this->display();
    }

    /*
     * Function:        personalOrderList
     * Description:    用户私教课列表
     */
    public function personalOrderList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $cate_id = 2;
        if (!empty($cate_id)) {
            $filter['cate_id'] = $cate_id;
        }
        $filter['course_id'] = I('course_id/d', '');
        if (!empty($filter['export'])) {
            D('UserCourse')->exportUserCourse($filter);
            exit;
        }
        $filter = returnFilter($filter);
        $list = D('UserCourse')->getUserCourseList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $val['user_name'] = M('user')->where(['id' => $val['user_id']])->getField('username');
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
                $userCardInfo = M('user_card')->where(['id' => $val['user_card_id']])->find();
                $val['goods_title'] = M('goods')->where(['id' => $userCardInfo['goods_id']])->getField('goods_title');
            }
        }
        $count = D('UserCourse')->getUserCourseCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $coachList = M('stores_worker_list')->where(['role' => 1, 'status' => 1, 'is_deleted' => 0])->select();
        $this->assign('coachList', $coachList);
        $this->display();
    }

    /*
     * Function:        instrumentOrderList
     * Description:    用户仪器课列表
     */
    public function instrumentOrderList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $cate_id = 3;
        if (!empty($cate_id)) {
            $filter['cate_id'] = $cate_id;
        }
        $filter['course_id'] = I('course_id/d', '');
        if (!empty($filter['export'])) {
            D('UserCourse')->exportUserCourse($filter);
            exit;
        }
        $filter = returnFilter($filter);
        $list = D('UserCourse')->getUserCourseList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $val['user_name'] = M('user')->where(['id' => $val['user_id']])->getField('username');
                $val['coach_name'] = M('stores_worker_list')->where(['id' => $val['coach_id'], 'role' => 1])->getField('name');
                $val['classroom_name'] = M('stores_classroom_list')->where(['id' => $val['classroom_id']])->getField('name');
                $userCardInfo = M('user_card')->where(['id' => $val['user_card_id']])->find();
                $val['goods_title'] = M('goods')->where(['id' => $userCardInfo['goods_id']])->getField('goods_title');
            }
        }
        $count = D('UserCourse')->getUserCourseCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $coachList = M('stores_worker_list')->where(['role' => 1, 'status' => 1, 'is_deleted' => 0])->select();
        $this->assign('coachList', $coachList);
        $this->display();
    }

    /*
     * Function:        changeCardList
     * Description:    卡变更列表
     */
    public function changeCardList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter = returnFilter($filter);
        $list = D('UserCardChangeLogs')->getUserCardChangeLogsList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $user_name = M('user')->where(['id' => $val['user_id']])->getField('username');
                $val['user_name'] = emojiDecode($user_name);
                $val['b_card_name'] = M('goods')->where(['id' => $val['goods_id']])->getField('goods_title');
                $val['card_name'] = M('goods')->where(['id' => $val['c_goods_id']])->getField('goods_title');
            }
        }
        $count = D('UserCardChangeLogs')->getUserCardChangeLogsCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        costCardList
     * Description:    卡消费列表
     */
    public function costCardList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter = returnFilter($filter);
        $list = D('UserCardLogs')->getUserCardLogsList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $user_name = M('user')->where(['id' => $val['user_id']])->getField('username');
                $val['user_name'] = emojiDecode($user_name);
                $userCardInfo = M('user_card')->where(['id' => $val['tid']])->find();
                $val['goods_title'] = M('goods')->where(['id' => $userCardInfo['goods_id']])->getField('goods_title');
                if ($val['type'] == 1) {
                    $val['after_nums'] = $val['now_nums'] + $val['nums'];
                } else {
                    $val['after_nums'] = $val['now_nums'] - $val['nums'];
                }
            }
        }
        $count = D('UserCardLogs')->getUserCardLogsCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $goodsList = M('goods')->where(['type' => 2, 'status' => 1, 'is_deleted' => 0])->select();
        $this->assign('goodsList', $goodsList);
        $this->display();
    }
}
